package com.kun.lagou.seckillservice.controller;

import com.kun.lagou.commodity.api.CommodityApi;
import com.kun.lagou.commodity.api.dto.CommodityInfo;
import com.kun.lagou.seckillservice.dto.ActivityInfo;
import com.kun.lagou.seckillservice.service.SeckillService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 秒杀
 * @author Administrator
 */
@RestController
@RequiredArgsConstructor
public class SeckillController {
    /**商品服务对外API*/
    private final CommodityApi commodityApi;
    /**秒杀服务*/
    private final SeckillService seckillService;

    /**
     * 根据商品ID封装活动信息
     * @param id
     * @return
     */
    @GetMapping("/activities/{id}")
    public ActivityInfo getActivityById(@PathVariable("id") long id) {
        CommodityInfo commodityInfo = commodityApi.getById(1);
        return ActivityInfo.builder()
                .id(1)
                .activityName("秒杀活动")
                .activityDesc("双十二秒杀")
                .commodityId(commodityInfo.getId())
                .commodityName(commodityInfo.getCommodityName())
                .commodityDesc(commodityInfo.getCommodityDesc())
                .build();
    }

    /**
     * 抢购
     * @param activityId
     * @param userId
     * @return
     */
    @PostMapping("/activities/{activityId}/{userId}/buy")
    public boolean buy(@PathVariable("activityId") long activityId, @PathVariable("userId") long userId) {
        return seckillService.buy(activityId, userId);
    }

    /**
     * 抢购——lua脚本
     * @param activityId
     * @param userId
     * @return
     */
    @PostMapping("/activities/{activityId}/{userId}/buyTwo")
    public boolean buy2(@PathVariable("activityId") long activityId, @PathVariable("userId") long userId) {
        return seckillService.buyTwo(activityId, userId);
    }
}
